using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Model
{
    public abstract class BaseValidatableObject : IValidatableObject
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            return CustomValidate(validationContext);
        }
        public abstract IEnumerable<ValidationResult> CustomValidate(ValidationContext validationContext);

        public bool IsValid
        {
            get
            {
                ValidationContext context = new ValidationContext(this, null, null);
                return Validator.TryValidateObject(this, context, _Results, true);
            }
        }

        public BaseValidatableObject()
        {
            _Results = new List<ValidationResult>();
        }

        private List<ValidationResult> _Results;
        public List<ValidationResult> BrokenRules { get { return _Results; } }

        public string BrokenRulesAsString
        {
            get
            {
                StringBuilder sbBrokenRules = new StringBuilder();

                if (_Results != null)
                {
                    foreach (var br in _Results)
                    {
                        sbBrokenRules.Append(br.ErrorMessage);
                        sbBrokenRules.Append(",");
                    }
                }

                return sbBrokenRules.ToString();
            }
        }

    }
}
